
#include <linux/version.h>
#include <linux/kernel.h>

#include <libv4l1-videodev.h>
#include "v4l_common.h"

#include "v4l.h"

/**
 * =============================================================================
 * =                      v4l functions implementation                         =
 * =============================================================================
 **/

/* = Getters ================================================================ */
int v4l1_get_video_capability(int fd, struct video_capability * vc) {
	return xioctl(fd, VIDIOCGCAP, vc);
}

int v4l1_get_video_channel(int fd, struct video_channel * vc) {
	return xioctl(fd, VIDIOCGCHAN, vc);
}

int v4l1_get_video_tuner(int fd, struct video_tuner * vt) {
	return xioctl(fd, VIDIOCGTUNER, vt);
}

int v4l1_get_video_picture(int fd, struct video_picture * vp) {
	return xioctl(fd, VIDIOCGPICT, vp);
}

int v4l1_get_video_window(int fd, struct video_window * vw) {
	return xioctl(fd, VIDIOCGWIN, vw);
}

int v4l1_get_video_buffer(int fd, struct video_buffer * vb) {
	return xioctl(fd, VIDIOCGFBUF, vb);
}

int v4l1_get_video_tuner_freq(int fd, unsigned long * freq) {
	return xioctl(fd, VIDIOCGFREQ, freq);
}

int v4l1_get_video_audio(int fd, struct video_audio * va) {
	return xioctl(fd, VIDIOCGAUDIO, va);
}

int v4l1_get_video_mbuf(int fd, struct video_mbuf * vmb) {
	return xioctl(fd, VIDIOCGMBUF, vmb);
}

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)

int v4l1_get_video_unit(int fd, struct video_unit * vu) {
	return xioctl(fd, VIDIOCGUNIT, vu);
}

int v4l1_get_video_capture(int fd, struct video_capture * vc) {
	return xioctl(fd, VIDIOCGCAPTURE, vc);
}

int v4l1_get_video_info(int fd, struct video_info * vi) {
	return xioctl(fd, VIDIOCGPLAYINFO, vi);
}
#endif

int v4l1_get_vbi_format(int fd, struct vbi_format * vf) {
	return xioctl(fd, VIDIOCGVBIFMT, vf);
}

/* = Setters ================================================================ */

int v4l1_set_video_channel(int fd, struct video_channel * vc) {
	return xioctl(fd, VIDIOCSCHAN, vc);
}

int v4l1_set_video_tuner(int fd, struct video_tuner * vt) {
	return xioctl(fd, VIDIOCSTUNER, vt);
}

int v4l1_set_video_picture(int fd, struct video_picture * vp) {
	return xioctl(fd, VIDIOCSPICT, vp);
}

int v4l1_set_capture(int fd, int * cap) {

	return xioctl(fd, VIDIOCCAPTURE, cap);
}

int v4l1_set_video_window(int fd, struct video_window * vw) {
	return xioctl(fd, VIDIOCSWIN, vw);
}

int v4l1_set_video_buffer(int fd, struct video_buffer * vb) {
	return xioctl(fd, VIDIOCSFBUF, vb);
}

int v4l1_set_video_tuner_freq(int fd, unsigned long * freq) {
	return xioctl(fd, VIDIOCSFREQ, freq);
}

int v4l1_set_video_audio(int fd, struct video_audio * va) {
	return xioctl(fd, VIDIOCSAUDIO, va);
}

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)

int v4l1_set_video_capture(int fd, struct video_capture * vc) {
	return xioctl(fd, VIDIOCSCAPTURE, vc);
}

int v4l1_set_video_play_mode(int fd, struct video_play_mode * vpm) {
	return xioctl(fd, VIDIOCSPLAYMODE, vpm);
}

int v4l1_set_video_write_mode(int fd, int * mode) {
	return xioctl(fd, VIDIOCSWRITEMODE, mode);
}

int v4l1_set_video_code(int fd, struct video_code * vc) {
	return xioctl(fd, VIDIOCSMICROCODE, vc);
}

#endif

int v4l1_set_vbi_format(int fd, struct vbi_format * vf) {
	return xioctl(fd, VIDIOCSVBIFMT, vf);
}



/* = Others ================================================================= */
int v4l1_start_capture(int fd) {
	int tmp = 1;
	return v4l1_set_capture(fd, &tmp);
}

int v4l1_stop_capture(int fd) {
	return v4l1_set_capture(fd, 0);
}

int v4l1_grab_video_mmap(int fd, struct video_mmap * vmm) {
	return xioctl(fd, VIDIOCMCAPTURE, vmm);
}

int v4l1_sync_frame(int fd, int * frame) {
	return xioctl(fd, VIDIOCSYNC, frame);
}

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0)
int v4l1_do_video_key(int fd, struct video_key * vk) {

	return xioctl(fd, VIDIOCKEY, vk);
}
#endif